package com.hl.ruoyicsmshop.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.hl.ruoyicsmshop.domain.UserCoupons;
import com.hl.ruoyicsmshop.domain.vo.UserCouponVO;
import com.hl.ruoyicsmshop.service.IUserCouponService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 用户优惠券Controller
 * 
 * @author ruoyi
 * @date 2025-01-27
 */
@RestController
@RequestMapping("/usercoupon")
public class UserCouponController extends BaseController
{
    @Autowired
    private IUserCouponService userCouponService;

    /**
     * 查询用户优惠券列表
     */
    // @RequiresPermissions("csmshop:usercoupon:list")
    @GetMapping("/list")
    public TableDataInfo list(UserCoupons userCoupons)
    {
        startPage();
        List<UserCoupons> list = userCouponService.selectUserCouponList(userCoupons);
        return getDataTable(list);
    }

    /**
     * 获取用户优惠券详细信息列表（包含优惠券详情）
     * 这是主要的接口，用于获取用户的优惠券信息
     */
    @GetMapping("/user/{userId}")
    public AjaxResult getUserCouponList(@PathVariable("userId") Long userId)
    {
        List<UserCouponVO> list = userCouponService.selectUserCouponDetailList(userId);
        return success(list);
    }

    /**
     * 导出用户优惠券列表
     */
    @RequiresPermissions("csmshop:usercoupon:export")
    @Log(title = "用户优惠券", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UserCoupons userCoupons)
    {
        List<UserCoupons> list = userCouponService.selectUserCouponList(userCoupons);
        ExcelUtil<UserCoupons> util = new ExcelUtil<UserCoupons>(UserCoupons.class);
        util.exportExcel(response, list, "用户优惠券数据");
    }

    /**
     * 获取用户优惠券详细信息
     */
    @RequiresPermissions("csmshop:usercoupon:query")
    @GetMapping(value = "/{userCouponId}")
    public AjaxResult getInfo(@PathVariable("userCouponId") Long userCouponId)
    {
        return success(userCouponService.selectUserCouponByUserCouponId(userCouponId));
    }

    /**
     * 新增用户优惠券
     */
    @RequiresPermissions("csmshop:usercoupon:add")
    @Log(title = "用户优惠券", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UserCoupons userCoupons)
    {
        return toAjax(userCouponService.insertUserCoupon(userCoupons));
    }

    /**
     * 修改用户优惠券
     */
    @RequiresPermissions("csmshop:usercoupon:edit")
    @Log(title = "用户优惠券", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UserCoupons userCoupons)
    {
        return toAjax(userCouponService.updateUserCoupon(userCoupons));
    }

    /**
     * 删除用户优惠券
     */
    @RequiresPermissions("csmshop:usercoupon:remove")
    @Log(title = "用户优惠券", businessType = BusinessType.DELETE)
	@DeleteMapping("/{userCouponIds}")
    public AjaxResult remove(@PathVariable Long[] userCouponIds)
    {
        return toAjax(userCouponService.deleteUserCouponByUserCouponIds(userCouponIds));
    }
}
